﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace FXCopAddIn.Classes
{
    public partial class ThreeStateTreeView : TreeView
    {
        #region Campos

        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.ImageList imgList;
        private System.Windows.Forms.ImageList imageListStateBase;

        #endregion

        #region Construtor

        public ThreeStateTreeView()
        {
            InitializeComponent();
        }

        #endregion

        #region Protected Override Methods

        protected override void OnClick(EventArgs e)
        {
            TreeNode node = null;

            base.OnClick(e);

            Point ponto = PointToClient(Control.MousePosition);
            TreeViewHitTestInfo info = this.HitTest(ponto.X, ponto.Y);
            if (info.Node != null && info.Location == TreeViewHitTestLocations.StateImage)
            {
                node = GetNodeAt(ponto);

                if (node != null)
                    MudaEstado((ThreeStateTreeNode)node);
            }
        }

        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            base.OnPreviewKeyDown(e);

            if (this.SelectedNode != null
                && e.KeyCode == Keys.Space)
            {
                MudaEstado((ThreeStateTreeNode)this.SelectedNode);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion

        #region Private Methods

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ThreeStateTreeView));
            this.imgList = new System.Windows.Forms.ImageList(this.components);
            this.imageListStateBase = new System.Windows.Forms.ImageList(this.components);
            this.SuspendLayout();
            // 
            // imgList
            // 
            this.imgList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgList.ImageStream")));
            this.imgList.TransparentColor = System.Drawing.Color.Transparent;
            this.imgList.Images.SetKeyName(0, "DllIcon.png");
            this.imgList.Images.SetKeyName(1, "FxCopIcon.bmp");
            this.imgList.Images.SetKeyName(2, "icon_csharp.gif");
            // 
            // imageListStateBase
            // 
            this.imageListStateBase.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListStateBase.ImageStream")));
            this.imageListStateBase.TransparentColor = System.Drawing.Color.Transparent;
            this.imageListStateBase.Images.SetKeyName(0, "checked_black.png");
            this.imageListStateBase.Images.SetKeyName(1, "checked_gray.png");
            this.imageListStateBase.Images.SetKeyName(2, "checked_white.png");
            // 
            // ThreeStateTreeView
            // 
            this.ImageIndex = 0;
            this.ImageList = this.imgList;
            this.SelectedImageIndex = 0;
            this.StateImageList = this.imageListStateBase;
            this.ResumeLayout(false);

        }

        private void MudaEstado(ThreeStateTreeNode treeNode)
        {
            ThreeStateTreeNode treeNodeParent;
            bool undefined = false;

            if (treeNode.Estado == Enums.ThreeState.Checked)
            {
                treeNode.Estado = Enums.ThreeState.Unchecked;
            }
            else
            {
                treeNode.Estado = Enums.ThreeState.Checked;
            }

            if (treeNode.Parent == null)
            {
                foreach (ThreeStateTreeNode node in treeNode.Nodes)
                {
                    node.Estado = treeNode.Estado;
                }
            }
            else
            {
                treeNodeParent = (ThreeStateTreeNode)treeNode.Parent;

                if (treeNode.Estado == Enums.ThreeState.Unchecked)
                {
                    foreach (ThreeStateTreeNode node in treeNodeParent.Nodes)
                    {
                        if (node.Estado == Enums.ThreeState.Checked)
                        {
                            undefined = true;
                            break;
                        }
                    }
                }
                else if (treeNode.Estado == Enums.ThreeState.Checked)
                {
                    foreach (ThreeStateTreeNode node in treeNodeParent.Nodes)
                    {
                        if (node.Estado == Enums.ThreeState.Unchecked)
                        {
                            undefined = true;
                            break;
                        }
                    }
                }

                if (undefined)
                {
                    treeNodeParent.Estado = Enums.ThreeState.Undefined;
                }
                else
                {
                    treeNodeParent.Estado = treeNode.Estado;
                }
            }
        }

        #endregion
    }
}
